<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;
use app\models\Department;
use app\models\Project;
use app\models\Report;
use app\models\ReportMonth;
use app\modules\v1\controllers\parent\ParentController;
use yii\data\ActiveDataProvider;
use Yii;

class ReportController extends ParentController
{
    public function actionIndex() {
        $month_start = date('Y-m-01', time());
        $month_end = date('Y-m-t', time());
        $start_time = strtotime(date('Y-01-01'));
        $params = [
            'start' =>  strtotime($month_start),
            'end' =>  strtotime($month_end)
        ];
//        $query = Report::find()->asArray()->with('department');
//        $result = $query->all();
//        return $result;

        $sectionOne = Department::getDepartmentByName('设计一室');
        $params['department_id'] = $sectionOne['id'];
        $yield = Project::contractSum($params, 'money_total');// 获取合同金额
        $reportMonthOne = ReportMonth::findOne(['createM_at' => strtotime($month_start), 'department_id' => $sectionOne['id']]);// 获取月份相关报表
        $reportYearOne = Report::find()->where(['department_id' => $sectionOne['id'], 'create_at' => $start_time])->one();// 获取年相关报表
        $branchOne = $this->projectTotal($sectionOne);// 获取相关统计数据
        $branchOne = array_merge($branchOne, ['month' => $reportMonthOne]);
        $branchOne = array_merge($branchOne, ['money_total' => $yield]);
        $branchOne = array_merge($branchOne, ['year_total' => $reportYearOne]);
        $sectionTwo = Department::getDepartmentByName('设计二室');
        $params['department_id'] = $sectionTwo['id'];
        $yield = Project::contractSum($params, 'money_total');
        $reportMonthTwo = ReportMonth::findOne(['createM_at' => strtotime($month_start), 'department_id' => $sectionTwo['id']]);
        $reportYearTwo = Report::findOne(['department_id' => $sectionTwo['id'], 'create_at' => $start_time]);// 获取年相关报表
        $branchTwo = $this->projectTotal($sectionTwo);
        $branchTwo = array_merge($branchTwo, ['month' => $reportMonthTwo]);
        $branchTwo = array_merge($branchTwo, ['money_total' => $yield]);
        $branchTwo = array_merge($branchTwo, ['year_total' => $reportYearTwo]);

        $sectionThree = Department::getDepartmentByName('设计三室');
        $params['department_id'] = $sectionThree['id'];
        $yield = Project::contractSum($params, 'money_total');
        $reportMonthThree = ReportMonth::findOne(['createM_at' => strtotime($month_start), 'department_id' => $sectionThree['id']]);
        $reportYearThree = Report::findOne(['department_id' => $sectionThree['id'], 'create_at' => $start_time]);// 获取年相关报表
        $branchThree = $this->projectTotal($sectionThree);
        $branchThree = array_merge($branchThree, ['month' => $reportMonthThree]);
        $branchThree = array_merge($branchThree, ['money_total' => $yield]);
        $branchThree = array_merge($branchThree, ['year_total' => $reportYearThree]);

        $sectionFour = Department::getDepartmentByName('技经室');
        $params['department_id'] = $sectionFour['id'];
        $yield = Project::contractSum($params, 'money_total');
        $reportMonthFour = ReportMonth::findOne(['createM_at' => strtotime($month_start), 'department_id' => $sectionFour['id']]);
        $reportYearFour = Report::findOne(['department_id' => $sectionFour['id'], 'create_at' => $start_time]);// 获取年相关报表
        $branchFour = $this->projectTotal($sectionFour);
        $branchFour = array_merge($branchFour, ['month' => $reportMonthFour]);
        $branchFour = array_merge($branchFour, ['money_total' => $yield]);
        $branchFour = array_merge($branchFour, ['year_total' => $reportYearFour]);



        return $result = [
            '1' => $branchOne,
            '2' => $branchTwo,
            '3' => $branchThree,
            '4' => $branchFour
        ];
    }

    // 按照部门的的相关统计
    public function projectTotal($params) {
        // 统计项目完成数量
        $completeNum = Report::completeNum($params['id'], 1); // 获取已完成项目
        $uncompleteNum = Report::completeNum($params['id'], 0);// 获取进行中项目
        $newProject = Report::newProject($params); //获取本月新建
        $receipt = Report::balanceMonth($params);// 获取本月收款额
        $yearReceipt = Report::balanceYear($params);// 获取年度收款

        return $arr = [
            'completeNum' => $completeNum,
            'uncompleteNum' => $uncompleteNum,
            'newProject' => $newProject,
            'receipt' => $receipt,
            'yearReceipt' => $yearReceipt
        ];
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate() {
        $data =  \Yii::$app->request->getBodyParams();//  1514736000
        if ($data['create_at']) {
            $data['create_at'] /=  1000;
        }
        $result = $this->createMonth($data);
        $yearReport = Report::find()->all(); // 获取年度报表
        if (!empty($yearReport)) {
            foreach ($yearReport as $K => $v) {
                if ($data['department_id'] == $v['department_id'] && $data['create_at'] == $v['create_at']) {
                    return [
                        'message' => $result,
                        'errors' => '该部门年度计划已有'
                    ];
                }
            }
        }
        $report = new Report();
        $report->load($data,'');

        if ($report->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $report,
                'message' => $result
            ];
        }
        return [
            'bCode' => Status::SUCCESS,
            'bData' => $report->errors,
            'message' => $result
        ];
    }

    public function createMonth($params) {
        $monthReport = ReportMonth::find() ->all();
        if ($params['createM_at']) {
            $params['createM_at'] /=  1000;
        }
        if (!empty($monthReport)) {
            foreach ($monthReport as $k => $v) {
                if ($params['department_id'] == $v['department_id'] && $v['createM_at'] == $params['createM_at']) {
                    return '该月份的生产计划已有';
                }
            }
        }

        $reportMonth = new ReportMonth();
        $reportMonth->load($params, '');
        if ($reportMonth->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $reportMonth,
            ];
        }
        return [
            'bCode' => Status::SUCCESS,
            'bData' => $reportMonth->errors,
        ];
    }
}